<template>
  <div style="height: 100%;width: 100%">
    <keep-alive>
      <el-row style="height: 100%;display: flex;width: 100%">
        <el-col :span="24" style="height: 100%;">
          <div style="height: 100%;">
            <el-row style="height: 150px;padding-top: 5px" type=“flex” align=“middle”>
              <SearchBox @searchByTitle="search"></SearchBox>
            </el-row>
            <el-row>
              <el-table
                :max-height="700" border
                v-loading="loading" :data="questionnaireList"
                :row-key="getRowKey"
                :expand-row-keys="expands"
                @expand-change="expandRow"
                ref="expandTable"
                @row-click="rowClick"
              >
                <el-table-column type="expand">
                  <template slot-scope="props">
                    <keep-alive>
                      <SubmitDetail
                        :submit-list="props.row.detailList"
                        :total="props.row.total"
                        :questionnaire-id="props.row.id"
                        style="margin-left: 300px"></SubmitDetail>
                    </keep-alive>
                  </template>
                </el-table-column>
                <el-table-column label="问卷编号" prop="id" align="center"
                                 :show-overflow-tooltip="true" width="200"
                />
                <el-table-column label="问卷名称" prop="title" align="center"
                                 :show-overflow-tooltip="true" width="400"
                />
                <el-table-column label="问卷创建时间" prop="createTime" align="center"
                                 :show-overflow-tooltip="true" width="400"
                />
                <el-table-column label="填写人数" prop="num" align="center"
                                 :show-overflow-tooltip="true" width="200"
                />
                <el-table-column label="操作" align="center">
                  <div slot-scope="scope">
                    <router-link :to="{path:'/QuestionAnalyse',query:{id:scope.row.id}}">
                      <el-button size="mini"
                                 @click="handleUpdate(scope.row)"
                      >可视化</el-button>
                    </router-link>
                  </div>

                </el-table-column>
              </el-table>

              <pagination v-show="total > 0"
                          :total="total" :page.sync="queryParams.pageNum"
                          :limit.sync="queryParams.pageSize" @pagination="getPage"
              />

              <question :open="openAdd" :datas="questionList" @exit="cancel"></question>
            </el-row>
          </div>
        </el-col>
        <div style="clear:both"></div>
      </el-row>

    </keep-alive>
  </div>
</template>
<script>
import Vertical from "@/layout/ManageMenu";
import Pagination from "@/components/Pagination";
import SearchBox from "@/views/reservation/search";
import SubmitDetail from "@/views/blog/SubmitDetail";
import {
  submitInfo,
  pageQuestionnaire,
  searchQuestionnaire
} from "@/api/questionnaire";
import Question from "@/views/consult/question";

export default {
  name: 'QuestionSubmit',
  components: {
    SubmitDetail,
    Question,
    SearchBox,
    Vertical,
    Pagination
  },
  data() {
    return {
      expands:[],
      id:0,
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showValue: true,
      // 总条数
      total: 0,
      // 弹出层标题
      diaTitle: "",
      // 是否显示弹出层
      open: false,

      openAdd:false,

      scoreOpen: false,
      scores: {},

      searchValue:false,
      // 日期范围
      dateRange: [],
      // 菜单列表
      menuOptions: [],
      // 部门列表
      deptOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 5,
        questionnaireId:null,
      },
      // 表单参数
      form: {},
      questionList: {
        itemList: [],
      },
      // 表单校验
      rules: {
        startTime: [{ required: true, message: "开始时间不能为空", trigger: "blur" }],
        endTime: [{ required: true, message: "结束时间不能为空", trigger: "blur" }],
        location: [{ required: true, message: "地点不能为空", trigger: "blur" }],
      },
      defaultProps: {
        children: "children",
        label: "label",
      },
      detailList:[],
      statusList:[
        {
          id:0,
          label:"待进行"
        },
        {
          id:1,
          label:"已完成"
        },
      ],
    };
  },
  created() {
    // this.getList();
    this.getPage(this.queryParams);
  },
  methods: {
    search(data){
      this.searchValue=data;
      this.loading=true;
      searchQuestionnaire({title:data,pageNum:this.queryParams.pageNum,pageSize:this.queryParams.pageSize})
        .then((response) => {
          if (response.success) {
            this.questionnaireList = response.data.records;
            this.total = response.data.total;//重要，会决定有几个页码按钮
            this.loading = false;
          }
        })
        .catch((e) => {
          _this.$message.error(e, "出现未知错误");
        });
    },
    //分页查询
    getPage(data) {
      if (this.searchValue){
        this.search(this.searchValue)
      }
      else{
        this.openAdd=false;
        this.loading = true;
        // this.queryParams.pageSize=10
        // this.openAdd=false;
        pageQuestionnaire(data)
          .then((response) => {
            if (response.success) {
              this.questionnaireList = response.data.records;
              if(this.total===0){
                this.total = response.data.total;//重要，会决定有几个页码按钮
              }
              // this.openAdd=false;
              this.loading = false;
              // this.openAdd=false;
            }
          })
          .catch((e) => {
            _this.$message.error(e, "出现未知错误");
          });
      }

    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.openAdd=false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        startTime: undefined,
        endTime:undefined,
        location:undefined,
      };
    },
    getRowKey(row){
      // alert(row.id)
      return row.id;
    },
    expandRow(row,expandRows){
      if(expandRows.length>0){
        this.expands=[]
        this.expands.push(row.id)
        // alert(JSON.stringify(row))
      }
      else {
        this.expands=[]
      }
      if(row.loading===undefined){
        this.$set(row,"loading",true);
        this.queryParams.questionnaireId=row.id;
        this.queryParams.pageSize=5
        submitInfo(this.queryParams)
          .then((response) => {
            if (response.success) {
              this.$set(row,"detailList",response.data.records)
              this.$set(row,"total",response.data.total)
              this.$set(row,"num",response.data.total)
              // alert(JSON.stringify(row))
              // alert(JSON.stringify(row.detailList))
            }
          })
          .catch((e) => {
            _this.$message.error(e, "出现未知错误");
          });
      }
      else {
        // alert("已经加载过数据了")
      }

    },
    rowClick(row){
      this.$refs.expandTable.toggleRowExpansion(row)
    }
  },
}
</script>

<style lang="scss" scoped>
.auto-generate-form {
  padding-top: 50px;
  padding-right: 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
</style>
